package main

type ListNode struct {
	Val  int
	Next *ListNode
}

func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	head := &ListNode{}
	n := head
	nextBit := 0
	for l1 != nil || l2 != nil || nextBit != 0 {
		v1 := 0
		v2 := 0
		if l1 != nil {
			v1 = l1.Val
			l1 = l1.Next
		}
		if l2 != nil {
			v2 = l2.Val
			l2 = l2.Next
		}
		result := v1 + v2 + nextBit
		v := result % 10
		nextBit = result / 10
		n.Next = &ListNode{
			Val:  v,
			Next: nil,
		}
		n = n.Next
	}
	return head.Next
}

func main() {
	n0 := &ListNode{Val: 2}
	n1 := &ListNode{Val: 4}
	n2 := &ListNode{Val: 3}
	n0.Next = n1
	n1.Next = n2

	m0 := &ListNode{Val: 5}
	m1 := &ListNode{Val: 6}
	m2 := &ListNode{Val: 4}
	m0.Next = m1
	m1.Next = m2

	ret := addTwoNumbers(n0, m0)
	println(ret)
}
